#include<iostream>

using namespace std;

bool find(int * arr, int rows, int cols, int key){
    bool result = false;
    // 获取右上角/左下角数据，作为比较对象
    int col = cols-1;
    int row = 0;
    while ( row < rows && col >=0 ){
        int flag = arr[row*cols+col];
        if ( key == flag ){
            result = true;
            break;
        }

        if ( key > flag){
            // 移动行，行下移
            row ++;
        }else {
            // 移动列，列左移
            col --;
        }
    }
    return result;
}

int main(void){
    int matrix[][4] = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
    int key = 7;
    bool result = find((int *)matrix, 4, 4, key);
    if (result){
        cout << "key 不存在" << endl;
    }else {
        cout << "key 存在" << endl;
    }
    return 0;
}